home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / rg10.zoo / rg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-17  |  7.6 KB  |  421 lines

  1. /*
  2.  *    rg - start a gem program under mint/tcsh
  3.  *
  4.  *    compile -mshort with mint libs
  5.  */
  6.  
  7. static char *rcsid = "$Id: rg.c,v 1.0 1992/06/15 02:14:30 rosenkra Exp $";
  8.  
  9. /*
  10.  * $Log: rg.c,v $
  11.  * Revision 1.0  1992/06/15  02:14:30  rosenkra
  12.  * Initial revision
  13.  * 
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <string.h>        /* for strcpy(), strcat() */
  18. #include <stdlib.h>        /* for exit() */
  19. #include <unistd.h>        /* for sleep() */
  20. #ifdef NEW_SCREEN
  21. #define __NO_MFDB__    /* avoid conflicts in gemfast.h and linea.h */
  22. #endif
  23. #include <gemdefs.h>
  24. #include <aesbind.h>
  25. #ifdef NEW_SCREEN
  26. #include <linea.h>
  27. #endif
  28. #include <vdibind.h>
  29. #include <osbind.h>
  30. #include <mintbind.h>
  31.  
  32.  
  33. /*
  34.  *    globals...
  35.  */
  36. long    _stksize = 16*1024L;        /* do we need this much? */
  37.  
  38. char   *uniterm_cmd =
  39. "E:\\LOCAL\\UNI20E\\UNITERM\\UNITERM.PRG\0                                   ";
  40.                     /* default command if no args */
  41.                     /* plenty of patch space... */
  42. int    debugging = 0;
  43. int    nogo = 0;
  44.  
  45. char    cmd[512];
  46. char    tail[512];
  47. #ifdef NEW_SCREEN
  48. char   *altscreen;            /* address of new screen */
  49. char   *alloc_mem;
  50. char   *phys, *log;
  51. #endif
  52. int    ap_id;
  53. int    screen_phandle;
  54. int    screen_vhandle;
  55. int    gr_wchar, gr_hchar, gr_wbox, gr_hbox;
  56. int    old_domain;
  57. char   *myname;
  58.  
  59.  
  60. /*
  61.  *    local functions...
  62.  */
  63. char   *prg_name ();
  64. int    open_vwork ();
  65. void    clr_screen ();
  66. int    run_prog ();
  67. void    quit ();
  68. void    usage ();
  69. #ifdef NEW_SCREEN
  70. char   *newscreen ();
  71. #endif
  72.  
  73.  
  74.  
  75. /*------------------------------*/
  76. /*    main            */
  77. /*------------------------------*/
  78. void main (int argc, char *argv[])
  79. {
  80.     int    excode;
  81.     
  82.  
  83.     /*
  84.      *   find our name. strip of any path
  85.      */
  86. #ifdef __GNUC__
  87.     myname = prg_name (argv[0]);
  88. #else
  89.     myname = "rg";
  90. #endif
  91.  
  92.  
  93.     /*
  94.      *   parse args. if none, use default (uniterm)
  95.      *
  96.      *   first look for - or + options...
  97.      */
  98.     for (argc--, argv++; (argc && ((**argv == '-') || (**argv == '+')));
  99.     argc--, argv++)
  100.     {
  101.         if (**argv == '+')
  102.         {
  103.             switch (*(*argv+1))
  104.             {
  105.             case 'v':            /* +version */
  106.                 fprintf (stderr, "%s\n", rcsid);
  107.                 exit (0);
  108.                 break; /*NOTREACHED*/
  109.  
  110.             case 'h':            /* +help */
  111.                 usage ();
  112.                 break; /*NOTREACHED*/
  113.  
  114.             case 'd':            /* +debug */
  115.                 debugging++;
  116.                 break;
  117.  
  118.             case 'n':            /* +nogo */
  119.                 nogo++;
  120.                 debugging++;
  121.                 break;
  122.             }
  123.         }
  124.         else
  125.         {
  126.             switch (*(*argv+1))
  127.             {
  128.             case 'v':            /* -v, version */
  129.                 fprintf (stderr, "%s\n", rcsid);
  130.                 exit (0);
  131.                 break; /*NOTREACHED*/
  132.  
  133.             case 'h':            /* -h, help */
  134.                 usage ();
  135.                 break; /*NOTREACHED*/
  136.  
  137.             case 'd':            /* -d, debug */
  138.                 debugging++;
  139.                 break;
  140.  
  141.             case 'n':            /* -n, nogo */
  142.                 nogo++;
  143.                 debugging++;
  144.                 break;
  145.             }
  146.         }
  147.     }
  148.  
  149.  
  150.     /*
  151.      *   any remaining args are command and (optional) tail. if no
  152.      *   more args, use default command and tail (uniterm)...
  153.      */
  154.     if (argc > 0)
  155.     {
  156.         strcpy (cmd, *argv);
  157.         for (argc--, argv++; (argc && *argv); argc--, argv++)
  158.         {
  159.             strcat (tail, *argv);
  160.             if (argc > 1)
  161.                 strcat (tail, " ");
  162.         }
  163.     }
  164.     else
  165.     {
  166.         strcpy (cmd, uniterm_cmd);
  167.         tail[0] = 0;
  168.     }
  169.     if (debugging)
  170.     {
  171.         fprintf (stderr, "%s: running:\t\"%s\"\n", myname, cmd);
  172.         fprintf (stderr, "%s: tail:\t\"%s\"\n", myname, tail);
  173.         if (!nogo)
  174.             sleep ((unsigned int) 2);
  175.     }
  176.     if (nogo)
  177.     {
  178.         exit (0);
  179.     }
  180.  
  181.  
  182.  
  183.     /*
  184.      *   The desktop is a TOS domain program, but the MiNT library sets up
  185.      *   MiNT domain by default, so we'd better change it back. save the
  186.      *   current domain for later reset...
  187.      */
  188.     old_domain = Pdomain (-1);
  189.     (void) Pdomain (0);
  190.  
  191.  
  192. #ifdef NEW_SCREEN
  193.     /*
  194.      *   allocate memory for new screen...
  195.      */
  196.     if ((altscreen = newscreen()) == NULL)
  197.     {
  198.         fprintf (stderr, "%s: could allocate memory for screen\n", myname);
  199.         quit (0, 1);
  200.     }
  201. #endif
  202.  
  203.  
  204.     /*
  205.      *   application init...
  206.      */
  207.     if ((ap_id = appl_init ()) < 0)
  208.     {
  209.         fprintf (stderr, "%s: could not do appl_init\n", myname);
  210.         quit (1, 2);
  211.     }
  212.  
  213.  
  214.     /*
  215.      *   get handle for screen...
  216.      */
  217.     screen_phandle = graf_handle (&gr_wchar, &gr_hchar, &gr_wbox, &gr_hbox);
  218.     
  219.  
  220.     /*
  221.      *   open virtual workstation and clear it...
  222.      */
  223.     if ((screen_vhandle = open_vwork (screen_phandle)) == 0)
  224.     {
  225.         quit (2, 3);
  226.     }
  227.     v_clrwk (screen_vhandle);
  228.  
  229.  
  230.     /*
  231.      *   hide cursor, show mouse...
  232.      */
  233.     Cconws ("\033f");        /* cursor off */
  234.     v_show_c (screen_vhandle, 0);    /* mouse on */
  235.  
  236.  
  237. #ifdef NEW_SCREEN
  238.     /*
  239.      *   save screen, set to altscreen...
  240.      */
  241.     log  = (char *) Logbase ();
  242.     phys = (char *) Physbase ();
  243.     Setscreen (altscreen, altscreen, -1);
  244. #endif
  245.  
  246.  
  247.     /*
  248.      *   do it...
  249.      */
  250.     excode = run_prog (cmd, tail);
  251.  
  252.  
  253. #ifdef NEW_SCREEN
  254.     /*
  255.      *   reset screen...
  256.      */
  257.     Setscreen (log, phys, -1);
  258. #endif
  259.  
  260.  
  261.     /*
  262.      *   show cursor, hide mouse...
  263.      */
  264.     v_hide_c (screen_vhandle);    /* mouse cursor off */
  265.     Cconws ("\033e");        /* cursor on */
  266.  
  267.  
  268.     /*
  269.      *   exit with status of command...
  270.      */
  271.     quit (10, excode);
  272. }
  273.  
  274.  
  275.  
  276. /*------------------------------*/
  277. /*    quit            */
  278. /*------------------------------*/
  279. void quit (int opt, int excode)
  280. {
  281.     switch (opt)
  282.     {
  283.     case 10:                /* normal exit */
  284.     case 4:
  285.                         /*  */
  286.                         /* FALLTHRU */
  287.  
  288.     case 3:
  289.         v_clrwk (screen_vhandle);    /* clear screen */
  290.         v_clsvwk (screen_vhandle);    /* close vwork */
  291.                         /* FALLTHRU */
  292.  
  293.     case 2:                    /* open_vwork failed... */
  294.         appl_exit ();            /* exit appl */
  295.                         /* FALLTHRU */
  296.  
  297.     case 1:                    /* appl_init failed ... */
  298. #ifdef NEW_SCREEN
  299.         Mfree (alloc_mem);        /* free screen memory */
  300. #endif
  301.                         /* FALLTHRU */
  302.  
  303.     case 0:                    /* newscreen failed... */
  304.         (void) Pdomain (old_domain);    /* reset domain */
  305.         clr_screen ();            /* clear screen */
  306.         exit (excode);            /* exit code */
  307.         break;                /* NOTREACHED */
  308.     }
  309. }
  310.  
  311.  
  312.  
  313. /*------------------------------*/
  314. /*    open_vwork        */
  315. /*------------------------------*/
  316. int open_vwork (int phandle)
  317. {
  318.     int    work_in[12];
  319.     int    work_out[60];
  320.     int    new_handle;
  321.     int    i;
  322.  
  323.     for (i = 0; i < 10; i++)
  324.         work_in[i] = 1;
  325.     work_in[10] = 2;
  326.     new_handle = phandle;
  327.  
  328.     v_opnvwk (work_in, &new_handle, work_out);
  329.  
  330.     return (new_handle);
  331. }
  332.  
  333.  
  334.  
  335. /*------------------------------*/
  336. /*    clr_screen        */
  337. /*------------------------------*/
  338. void clr_screen (void)
  339. {
  340.     Cconws ("\33E");        /* clear screen via GEMDOS */
  341. }
  342.  
  343.  
  344.  
  345. /*------------------------------*/
  346. /*    run_prog        */
  347. /*------------------------------*/
  348. int run_prog (char *rcmd, char *rtail)
  349. {
  350.     long    ret;
  351.  
  352.     ret = Pexec ((short) 0, (long) rcmd, (long) rtail, (long) 0);
  353.  
  354.     return ((int) ret);
  355. }
  356.  
  357.  
  358.  
  359. #ifdef NEW_SCREEN
  360. /*------------------------------*/
  361. /*    newscreen        */
  362. /*------------------------------*/
  363. char *newscreen (void)
  364. {
  365.     long    temp;
  366.     long    size;
  367.     char   *foo, *ret;
  368.  
  369.     linea0 ();
  370.     size = __aline->_VWRAP * V_Y_MAX;
  371.  
  372.     temp = Malloc (size + 512);
  373.     if (!temp)
  374.         return ((char *) 0);
  375.  
  376.     alloc_mem = (char *) temp;
  377.     foo = ret = (char *) ((temp + 511) & ~511);
  378.  
  379.     while (size-- > 0)    /* clear the screen */
  380.         *foo++ = 0;
  381.  
  382.     return ((char *) ret);
  383. }
  384. #endif /*NEW_SCREEN*/
  385.  
  386.  
  387.  
  388. /*------------------------------*/
  389. /*    prg_name        */
  390. /*------------------------------*/
  391. char *prg_name (char *av)
  392. {
  393. /*
  394.  *    MiNT can include full path in argv[0]. this finds just the name.
  395.  */
  396.     char   *ps1;
  397.     char   *ps2;
  398.  
  399.     ps1 = rindex (av, '/');
  400.     ps2 = rindex (av, '\\');
  401.     if ((long) ps1 > 0 && (long) ps1 > (long) ps2)
  402.         return (++ps1);
  403.     else if ((long) ps2 > 0 && (long) ps2 > (long) ps1)
  404.         return (++ps2);
  405.     else
  406.         return (av);
  407. }
  408.  
  409.  
  410.  
  411. /*------------------------------*/
  412. /*    usage            */
  413. /*------------------------------*/
  414. void usage ()
  415. {
  416. fprintf (stderr, "usage: %s [-n][-d][-h][-v] gem_program [tail]\n", myname);
  417. fprintf (stderr, "       %s [+nogo][+version][+debug][+help] ...\n", myname);
  418. exit (0);
  419. }
  420.  
  421.